/**
 * @author Administrator
 *
 */
package com.mblog.comment.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.mblog.comment.entity.MBlogComment;
import com.mblog.comment.service.ArticleCommentService;
import com.mblog.util.CusAccessAddressUtil;
import com.mblog.util.PageBean;

import net.sf.json.JSON;
import net.sf.json.JSONObject;

@Controller
@RequestMapping("/comment")
public class CommentController{

	static int pageSize=10;
	
	@Autowired
	private ArticleCommentService articleCommentService;
	//评论列表
	@RequestMapping(value="/goCommentsList.do")
	public  String goArticleList(HttpServletResponse responese,HttpServletRequest request){
		String pageNos=request.getParameter("currentPage");
		if(pageNos==null||"".equals(pageNos)){
			pageNos="1";
		}
		int pageNo=Integer.parseInt(pageNos);
		Map<String,Object> map=new HashMap<String,Object>();
		String articleId=request.getParameter("articleId");
		map.put("blogId", Integer.parseInt(articleId));
		//获取分页数据
		PageBean page=articleCommentService.selectAllCommentsByPage(pageNo, pageSize, map);
		List<MBlogComment> list=(List<MBlogComment>)page.getResult();
		for (int i = 0; i < list.size(); i++) {
			List<MBlogComment> quoteList=new ArrayList<MBlogComment>();
			MBlogComment comment=new MBlogComment();
			if(list.get(i).getCommentId()!=0){
				comment=list.get(i);
				AddComment(quoteList,comment);
				List<MBlogComment> pageList=new ArrayList<MBlogComment>();
				for (int j = 0; j < quoteList.size(); j++) {
					pageList.add(quoteList.get(quoteList.size()-1-j));
				}
				comment.setQuatoComment(pageList);
			};
		}
		page.setResult(list);
		request.setAttribute("Page", page);
		return "website/articledetailpage";
	}
	//递归获取回复评论
	public void AddComment(List<MBlogComment> quoteList, MBlogComment cmt){
		try {
				if(cmt.getCommentId()!=0){
					MBlogComment comment=articleCommentService.selectCommentsByParentid(cmt.getCommentId());
					quoteList.add(comment);
					AddComment(quoteList,comment);
				}else {
					return;
				}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	//提交评论
	@RequestMapping(value="/addComments.do")
	public  String addComments(HttpServletResponse responese,HttpServletRequest request,RedirectAttributes  attr){	
		String commentName=request.getParameter("author");
		String emailAddress=request.getParameter("email");
		String comment=request.getParameter("comment");
		String blogId=request.getParameter("blogId");
		String commentId=request.getParameter("commentId");
		String ip=CusAccessAddressUtil.getIpAddress(request);//获取用户ip地址
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("blogId", Integer.parseInt(blogId));
		int sort=articleCommentService.selectMaxSortByBlogId(map)+1;
		attr.addAttribute("articleId", blogId);
		try {
			MBlogComment comments=new MBlogComment();
			comments.setBlogId(Integer.parseInt(blogId));
			comments.setComentname(commentName);
			comments.setCommentdate(new Date());
			comments.setCommentemail(emailAddress);
			comments.setContent(comment);
			comments.setCommentId(Integer.parseInt(commentId));
			comments.setState(0);
			comments.setUserip(ip);
			comments.setSort(sort);
			articleCommentService.insert(comments);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "redirect:goCommentsList.do";
	}
}
